using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Tomahawk.Runtime.Serialization;
using Tomahawk.Runtime.Rendering.Objects;

namespace Tomahawk.Runtime.Logic
{
    public class XMaterialMeshComponent : XRenderableComponent
    {

        #region "Types"

        /// <summary>
        /// This struct is used to specify which material should be used with each mesh part
        /// </summary>
        public struct MaterialOverride
        {
            [XmlAttribute("meshpart")]
            public string MeshPartName;

            [XmlAttribute("material")]
            public string MaterialName;
        };

        #endregion

        protected XMaterialMeshComponent()
        {
            //default priority is: Scene
            this.RenderStage = Engine.RenderingStages.Scene;
        }

        #region "Serializable properties"

        private string meshName;
        
        /// <summary>
        /// Indicates the name of the 3D model that will be used
        /// </summary>
        [XmlElement("MeshName")]
        public string MeshName
        {
            get { return meshName; }
            set { meshName = value; }
        }

        private SerializableList<MaterialOverride> materialOverrides;

        /// <summary>
        /// Allows the model materials to be overriden by XEngine materials
        /// </summary>
        [XmlElement("MaterialOverrides")]
        public SerializableList<MaterialOverride> MaterialOverrides
        {
            get { return materialOverrides; }
            set { materialOverrides = value; }
        }

        #endregion

        protected MaterialMesh meshModel;

        /// <summary>
        /// Returns the rendering object used to render the model
        /// </summary>
        public MaterialMesh MeshModel
        {
            get { return meshModel; }
        }

        /// <summary>
        /// Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            // create a string array copy of the material overrides
            string[] meshParts = new string[this.materialOverrides.Count];
            string[] materials = new string[this.materialOverrides.Count];
            int i = 0;
            foreach (MaterialOverride matOv in this.materialOverrides)
            {
                meshParts[i] = matOv.MeshPartName;
                materials[i] = matOv.MaterialName;
                i++;
            }

            //create the model
            this.meshModel = new MaterialMesh(this.meshName, meshParts, materials);
            this.SceneNode.Attach(this.meshModel);

        }

        #region "Readyness"

        /// <summary>
        /// Returns true when all the resource dependant objects are ready
        /// </summary>
        protected override bool checkResourcesReady()
        {
            return base.checkResourcesReady() &&
                this.meshModel.IsReady;
        }

        #endregion

    }

}
